package controllers

import (
	"gitee.com/igolang/imoney/internal/app/system/services"
	"gitee.com/igolang/imoney/internal/common/httpResp"
	"github.com/gin-gonic/gin"
)

var (
	OperLog    = &operLog{}
	Logininfor = &logininfor{}
)

type (
	operLog    struct{}
	logininfor struct{}
)

func (c *logininfor) List(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := services.Logininfor.List(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *logininfor) Export(ctx *gin.Context) {
	r := httpResp.Response()
	b, err := services.Logininfor.Export(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		httpResp.JSON(ctx, r)
		return
	}

	httpResp.Export(ctx, b, "xlsx")
}

func (c *logininfor) Delete(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.Logininfor.Delete(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *logininfor) Clean(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.Logininfor.Clean(); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *operLog) List(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	data, err := services.OperLog.List(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
	r.Data = data
}

func (c *operLog) Export(ctx *gin.Context) {
	r := httpResp.Response()
	b, err := services.OperLog.Export(ctx)
	if err != nil {
		httpResp.ErrorResponse(err, r)
		httpResp.JSON(ctx, r)
		return
	}

	httpResp.Export(ctx, b, "xlsx")
}

func (c *operLog) Delete(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.OperLog.Delete(ctx); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}

func (c *operLog) Clean(ctx *gin.Context) {
	r := httpResp.Response()
	defer httpResp.JSON(ctx, r)
	if err := services.OperLog.Clean(); err != nil {
		httpResp.ErrorResponse(err, r)
		return
	}
}
